home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 April / macformat-023.iso / Shareware in MacFormat / brailler0.5b / brlr ƒ / Shell ƒ / other MSG window.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-20  |  9.6 KB  |  395 lines  |  [TEXT/MMCC]

  1. #include "other MSG window.h"
  2. #include "environment.h"
  3. #include "menus.h"
  4. #include "util.h"
  5. #include "file utilities.h"
  6. #include "main.h"
  7. #include "text twiddling.h"
  8. #include "graphics.h"
  9. #include "window layer.h"
  10. #include "program globals.h"
  11. #include <Folders.h>
  12.  
  13. enum { key_LeftArrow=0x1c, key_RightArrow, key_UpArrow, key_DownArrow };
  14. enum { key_PageUp=0x0b, key_PageDown };
  15. enum { key_Home=0x01 };
  16. enum { key_End=0x04 };
  17.  
  18. #define kGrowBoxSize        15
  19. #define PROGRAMS_LIST_NAME    "\pMSG programs list"
  20. #define kListResourceID        151
  21. #define LIST_CREATOR        'ttxt'
  22. #define LIST_TYPE            'ttro'
  23.  
  24. static    Boolean GetListFromDisk(void);
  25. static    Boolean SetupNewListFile(short fileID);
  26. static    Boolean GetListFromResource(void);
  27. static    unsigned long GetModificationDate(FSSpec *theFS);
  28. static    void PutProductListIntoTE(WindowPtr theWindow);
  29.  
  30. static    short            gOldForegroundTime;        /* stored foreground wait time */
  31. static    Boolean            gIsActive=FALSE;
  32. static    Handle            gTheList=0L;
  33. static    Boolean            gSetupDone=FALSE;
  34.  
  35. void SetupTheOtherMSGWindow(WindowPtr theWindow)
  36. {
  37.     unsigned char    *titleStr="\pOther MSG products";
  38.     Point            topLeft;
  39.     
  40.     SetWindowHeight(theWindow,
  41.         qd.screenBits.bounds.bottom-qd.screenBits.bounds.top-LMGetMBarHeight()-28);
  42.     SetWindowWidth(theWindow, qd.screenBits.bounds.right-qd.screenBits.bounds.left-70);
  43.     SetWindowType(theWindow, zoomDocProc);
  44.     topLeft.v=qd.screenBits.bounds.top+LMGetMBarHeight()+20;
  45.     topLeft.h=qd.screenBits.bounds.left+10;
  46.     SetWindowTopLeft(theWindow, topLeft);
  47.     SetWindowHasCloseBox(theWindow, TRUE);
  48.     SetWindowMaxDepth(theWindow, 1);
  49.     SetWindowDepth(theWindow, 1);
  50.     SetWindowAutoCenter(theWindow, FALSE);
  51.     SetWindowTitle(theWindow, titleStr);
  52.     SetWindowIsFloat(theWindow, FALSE);
  53.     
  54.     if (gSetupDone)
  55.         return;
  56.     
  57.     if (!GetListFromDisk())
  58.         GetListFromResource();
  59. }
  60.  
  61. void OpenTheOtherMSGWindow(WindowPtr theWindow)
  62. {
  63.     TEHandle        hTE;
  64.     FontInfo        theFontInfo;
  65.     Rect            vScrollBarRect, hScrollBarRect;
  66.     Rect            destRect, viewRect;
  67.     
  68.     hTE=GetWindowTE(theWindow);
  69.     if (hTE==0L)
  70.     {
  71.         SetRect(&vScrollBarRect, GetWindowWidth(theWindow)-kGrowBoxSize, -1,
  72.             GetWindowWidth(theWindow)+1, GetWindowHeight(theWindow)+1-kGrowBoxSize);
  73.         SetRect(&hScrollBarRect, -1, GetWindowHeight(theWindow)-kGrowBoxSize,
  74.             GetWindowWidth(theWindow)-kGrowBoxSize+1, GetWindowHeight(theWindow)+1);
  75.         SetWindowVScrollBar(theWindow,
  76.             NewControl(theWindow, &vScrollBarRect, "\p", TRUE, 0, 0, 0, scrollBarProc, 0));
  77.         SetWindowHScrollBar(theWindow,
  78.             NewControl(theWindow, &hScrollBarRect, "\p", TRUE, 0, 0, 0, scrollBarProc, 0));
  79.         
  80.         GetTERect(theWindow, &destRect, TRUE);
  81.         viewRect=destRect;
  82.         hTE=TENew(&destRect, &viewRect);
  83.         SetWindowTE(theWindow, hTE);
  84.         TextFont((**hTE).txFont=36);
  85.         TextSize((**hTE).txSize=12);
  86.         TextFace((**hTE).txFace=0);
  87.         GetFontInfo(&theFontInfo);
  88.         (**hTE).fontAscent=theFontInfo.ascent;
  89.         (**hTE).lineHeight=theFontInfo.ascent+theFontInfo.descent+theFontInfo.leading;
  90.         AdjustViewRect(hTE);
  91.         TEAutoView(TRUE, hTE);
  92.         TESetClickLoop((ProcPtr)MyClikLoop, hTE);
  93.         PutProductListIntoTE(theWindow);
  94.     }
  95.     
  96.     gIsActive=TRUE;
  97.     AdjustVScrollBar(GetWindowVScrollBar(theWindow), hTE);
  98.     AdjustMenus();
  99. }
  100.  
  101. void KeyPressedInOtherMSGWindow(WindowPtr theWindow, unsigned char theChar)
  102. {
  103.     TEHandle        hTE;
  104.     ControlHandle    vScrollBar;
  105.     
  106.     if (gInProgress)
  107.         return;
  108.     
  109.     hTE=GetWindowTE(theWindow);
  110.     vScrollBar=GetWindowVScrollBar(theWindow);
  111.     
  112.     switch (theChar)
  113.     {
  114.         case key_PageUp:
  115.             ScrollActionProc(vScrollBar, inPageUp);
  116.             break;
  117.         case key_PageDown:
  118.             ScrollActionProc(vScrollBar, inPageDown);
  119.             break;
  120.         case key_Home:
  121.             TEPinScroll(0, TEGetHeight((**hTE).nLines, 1, hTE), hTE);
  122.             break;
  123.         case key_End:
  124.             TEPinScroll(0, -TEGetHeight((**hTE).nLines, 1, hTE), hTE);
  125.             break;
  126.         default:
  127.             break;
  128.     }
  129.     
  130.     AdjustVScrollBar(vScrollBar, hTE);
  131. }
  132.  
  133. void MouseClickedInOtherMSGWindow(WindowPtr theWindow, Point thePoint)
  134. {
  135.     short            partCode;
  136.     ControlHandle    theControl;
  137.     short            scrollDistance;
  138.     short            oldSetting;
  139.     ControlActionUPP    scrollActionUPP=NewControlActionProc(ScrollActionProc);
  140.     TEHandle        hTE;
  141.     
  142.     if (gInProgress)
  143.         return;
  144.     
  145.     partCode=FindControl(thePoint, theWindow, &theControl);
  146.     if (theControl==GetWindowVScrollBar(theWindow))
  147.     {
  148.         switch (partCode)
  149.         {
  150.             case inThumb:
  151.                 oldSetting=GetControlValue(theControl);
  152.                 partCode=TrackControl(theControl, thePoint, 0L);
  153.                 if (partCode==inThumb)
  154.                 {
  155.                     scrollDistance=oldSetting-GetControlValue(theControl);
  156.                     if (scrollDistance!=0)
  157.                     {
  158.                         hTE=GetWindowTE(theWindow);
  159.                         TEPinScroll(0, scrollDistance*(**hTE).lineHeight, hTE);
  160.                     }
  161.                 }
  162.                 break;
  163.             case inUpButton:
  164.             case inDownButton:
  165.             case inPageUp:
  166.             case inPageDown:
  167.                 partCode=TrackControl(theControl, thePoint, scrollActionUPP);
  168.                 break;
  169.         }
  170.     }
  171. }
  172.  
  173. void DisposeTheOtherMSGWindow(WindowPtr theWindow)
  174. {
  175.     TEHandle        hTE;
  176.     
  177.     hTE=GetWindowTE(theWindow);
  178.     if (hTE!=0L)
  179.     {
  180.         TEDispose(hTE);
  181.         SetWindowTE(theWindow, 0L);
  182.     }
  183. }
  184.  
  185. void ActivateTheOtherMSGWindow(WindowPtr theWindow)
  186. {
  187.     gOldForegroundTime=gForegroundWaitTime;
  188.     gForegroundWaitTime=0;
  189.     TEActivate(GetWindowTE(theWindow));
  190.     gIsActive=TRUE;
  191.     HiliteControl(GetWindowVScrollBar(theWindow), 0);
  192.     HiliteControl(GetWindowHScrollBar(theWindow), 0);
  193.     DrawGrowIcon(theWindow);
  194. }
  195.  
  196. void DeactivateTheOtherMSGWindow(WindowPtr theWindow)
  197. {
  198.     Rect            tempRect;
  199.     
  200.     gForegroundWaitTime=gOldForegroundTime;
  201.     TEDeactivate(GetWindowTE(theWindow));
  202.     gIsActive=FALSE;
  203.     gCustomCursor=FALSE;
  204.     HiliteControl(GetWindowVScrollBar(theWindow), 255);
  205.     HiliteControl(GetWindowHScrollBar(theWindow), 255);
  206.     tempRect.bottom=theWindow->portRect.bottom;
  207.     tempRect.right=theWindow->portRect.right;
  208.     tempRect.left=tempRect.right-kGrowBoxSize+1;
  209.     tempRect.top=tempRect.bottom-kGrowBoxSize+1;
  210.     EraseRect(&tempRect);
  211. }
  212.  
  213. void CopybitsTheOtherMSGWindow(WindowPtr theWindow, WindowPtr offscreenWindowPtr)
  214. {
  215.     Rect            tempRect;
  216.     
  217.     if (gIsActive)
  218.     {
  219.         DrawGrowIcon(theWindow);
  220.     }
  221.     else
  222.     {
  223.         tempRect.bottom=theWindow->portRect.bottom;
  224.         tempRect.right=theWindow->portRect.right;
  225.         tempRect.left=tempRect.right-kGrowBoxSize+1;
  226.         tempRect.top=tempRect.bottom-kGrowBoxSize+1;
  227.         EraseRect(&tempRect);
  228.     }
  229.     
  230.     UpdateControls(theWindow, theWindow->visRgn);
  231.     
  232.     tempRect=theWindow->portRect;
  233.     tempRect.bottom-=kGrowBoxSize;
  234.     tempRect.right-=kGrowBoxSize;
  235.     CopyBits(    &(offscreenWindowPtr->portBits),
  236.                 &(theWindow->portBits),
  237.                 &tempRect, &tempRect, 0, 0L);
  238. }
  239.  
  240. void ResizeControlsInOtherMSGWindow(WindowPtr theWindow)
  241. {
  242.     TEHandle        hTE;
  243.     ControlHandle    vScrollBar, hScrollBar;
  244.     
  245.     hTE=GetWindowTE(theWindow);
  246.     vScrollBar=GetWindowVScrollBar(theWindow);
  247.     hScrollBar=GetWindowHScrollBar(theWindow);
  248.     AdjustScrollSizes(theWindow, hTE, vScrollBar, hScrollBar);
  249.     AdjustViewRect(hTE);
  250.     TECalText(hTE);
  251.     AdjustForEndScroll(vScrollBar, hTE);
  252.     AdjustVScrollBar(vScrollBar, hTE);
  253. }
  254.  
  255. void GetGrowSizeOtherMSGWindow(WindowPtr theWindow, Rect *sizeRect)
  256. {
  257.     SetRect(sizeRect, 200, 48+kGrowBoxSize+1, 32766, 32767);
  258. }
  259.  
  260. /* ---------------------------------------------------- */
  261. /* the rest of these are internal to other MSG window.h */
  262.  
  263. static    void PutProductListIntoTE(WindowPtr theWindow)
  264. {
  265.     long            listEOF;
  266.     
  267.     if (gTheList==0L)
  268.         return;
  269.     
  270.     listEOF=GetHandleSize(gTheList);
  271.     HLock(gTheList);
  272.     SetTheText(theWindow, *gTheList, listEOF);
  273.     HUnlock(gTheList);
  274.     TESetSelect(0, 0, GetWindowTE(theWindow));
  275. }
  276.  
  277. static    Boolean GetListFromDisk(void)
  278. {
  279.     Boolean            isNewFile;
  280.     unsigned char    *name=PROGRAMS_LIST_NAME;
  281.     OSErr            isHuman;
  282.     short            vRefNum;
  283.     long            dirID;
  284.     FSSpec            listFileFS, progFS;
  285.     short            listFile;
  286.     long            listEOF;
  287.     unsigned long    diskListModDate, ourListModDate;
  288.     
  289.     isNewFile=FALSE;
  290.     /* find vRefNum and dirID of preferences folder, creating it if necessary */
  291.     isHuman=FindFolder(kOnSystemDisk, 'pref', kCreateFolder, &vRefNum, &dirID);
  292.     
  293.     if (isHuman!=noErr)        /* screwed up already?!? */
  294.         return FALSE;
  295.     
  296.     isHuman=FSMakeFSSpec(vRefNum, dirID, name, &listFileFS);    /* make FSSpec out of it */
  297.     if (isHuman!=noErr)
  298.     {
  299.         if (isHuman==fnfErr)    /* FSSpec is valid, but list file does not exist */
  300.         {
  301.             isHuman=FSpCreate(&listFileFS, LIST_CREATOR, LIST_TYPE, 0);    /* so create it */
  302.             if (isHuman!=noErr)                                        /* or not */
  303.                 return FALSE;
  304.             isNewFile=TRUE;        /* signal that list file is new */
  305.         }
  306.         else return FALSE;
  307.     }
  308.     isHuman=FSpOpenDF(&listFileFS, fsRdWrPerm, &listFile);    /* open list file */
  309.     if (isHuman!=noErr)
  310.         return FALSE;
  311.     
  312.     diskListModDate=GetModificationDate(&listFileFS);
  313.     GetApplicationFSSpec(&progFS);
  314.     ourListModDate=GetModificationDate(&progFS);
  315.     if (diskListModDate<ourListModDate)
  316.         isNewFile=TRUE;
  317.     
  318.     if (isNewFile)
  319.     {
  320.         if (!SetupNewListFile(listFile))
  321.         {
  322.             FSClose(listFile);
  323.             FSpDelete(&listFileFS);
  324.             return FALSE;
  325.         }
  326.         SetModificationDate(&listFileFS, ourListModDate);
  327.     }
  328.     else
  329.     {
  330.         GetEOF(listFile, &listEOF);
  331.         gTheList=NewHandle(listEOF);
  332.         if (gTheList==0L)
  333.         {
  334.             FSClose(listFile);
  335.             FSpDelete(&listFileFS);
  336.             return FALSE;
  337.         }
  338.         HLock(gTheList);
  339.         SetFPos(listFile, 1, 0L);
  340.         if (FSRead(listFile, &listEOF, *gTheList)!=noErr)
  341.         {
  342.             FSClose(listFile);
  343.             FSpDelete(&listFileFS);
  344.             DisposeHandle(gTheList);
  345.             gTheList=0L;
  346.             return FALSE;
  347.         }
  348.         HUnlock(gTheList);
  349.     }
  350.     
  351.     FSClose(listFile);
  352. //    FlushVol(dirID, vRefNum);
  353.     
  354.     return TRUE;
  355. }
  356.  
  357. static    Boolean SetupNewListFile(short fileID)
  358. {
  359.     long            listEOF;
  360.     
  361.     if (!GetListFromResource())
  362.         return FALSE;
  363.     
  364.     listEOF=GetHandleSize(gTheList);
  365.     if (SetEOF(fileID, listEOF)!=noErr)
  366.         return FALSE;
  367.     
  368.     SetFPos(fileID, 1, 0L);
  369.     HLock(gTheList);
  370.     if (FSWrite(fileID, &listEOF, *gTheList)!=noErr)
  371.         return FALSE;
  372.     HUnlock(gTheList);
  373.     
  374.     return TRUE;
  375. }
  376.  
  377. static    Boolean GetListFromResource(void)
  378. {
  379.     gTheList=(Handle)Get1Resource('TEXT', kListResourceID);
  380.     if (gTheList==0L)
  381.         return FALSE;
  382.     if (*gTheList==0L)
  383.         LoadResource(gTheList);
  384.     if (*gTheList==0L)
  385.     {
  386.         DisposeHandle(gTheList);
  387.         gTheList=0L;
  388.         return FALSE;
  389.     }
  390.     
  391.     DetachResource(gTheList);
  392.     
  393.     return TRUE;
  394. }
  395.